package yawning.mew.cat;

import yawning.mew.sim.SimulationAction;

public class FeralChargeAction implements SimulationAction {
	CatModelSimState mState;
	double mCooldownExpireTime;

	FeralChargeAction(CatModelSimState state) {
		mState = state;
		if (mState.mModel.druid.getTalent(CatModel.FERALCHARGE) == 0) mCooldownExpireTime = Double.MAX_VALUE;
	}

	@Override
	public double perform() {
		int cost = mState.mModel.feralChargeCost;

		if (mState.mActiveTarget == null) return SimulationAction.ACTION_FAIL;
		if (mCooldownExpireTime > mState.mSimEngine.getSimTime() || mState.getEnergy() < cost) return SimulationAction.ACTION_FAIL;

		// If we are in combat, ensure that we have range.
		if (mState.isInCombat() && mState.isInMeleeRange()) {
			return SimulationAction.ACTION_FAIL;
		}

		int prevEnergy = mState.getEnergy();

		if (mState.mIsLogging) mState.mLog.log("Feral Charge\n");

		mCooldownExpireTime = mState.mSimEngine.getSimTime() + mState.mModel.feralChargeCDDuration;
		mState.mStampedeEv.run();

		mNrFeralCharge++;

		mState.mIsInMeleeRange = true;
		mState.onEnergyConsumer(cost);

		mState.logEnergyChange(prevEnergy);

		return 0.0;
	}

	int mNrFeralCharge = 0;
}
